package com.opcode.costa.ui.splashscreen;

import com.opcode.nebulamobil.base.services.ConnectionService;
import com.opcode.nebulamobil.base.services.NebulaFinderService;
import com.opcode.nebulamobil.base.services.PushTalkService;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootUpReceiver extends BroadcastReceiver{

	//Referanslar
	//Intent service,pushTalkService,nebulaFinderService;
    @Override
    public void onReceive(Context context, Intent intent) {
            Intent i = new Intent(context, SplashActivity.class);  
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);  
            
    /*        this.service = new Intent(context, ConnectionService.class);
        	this.pushTalkService=new Intent(context,PushTalkService.class);
        	this.nebulaFinderService=new Intent(context,NebulaFinderService.class);

         	
         	if(isMyServiceRunning(ConnectionService.class,context)){
         		Log.i("BootUpReceiver", "ConnectionService arka planda alışıyor.");

         		context.stopService(service);
         		context.startService(service);

         	}else{
         		context.startService(service);
         		Log.i("BootUpReceiver", "ConnectionService başlatılıyor.");
         	}
         	
         	
         	if(isMyServiceRunning(PushTalkService.class,context)){
         		Log.i("BootUpReceiver", "PushTalkService arka planda alışıyor.");
         		context.stopService(pushTalkService);
         		context.startService(pushTalkService);
         	}else{
         		context.startService(pushTalkService);
         		Log.i("BootUpReceiver", "PushTalkService başlatılıyor.");
         	}
         	
         	
        	if(isMyServiceRunning(NebulaFinderService.class,context)){
         		Log.i("BootUpReceiver", "NebulaFinderService arka planda alışıyor.");
         		context.stopService(nebulaFinderService);
         		context.startService(nebulaFinderService);
         	}else{
         		context.startService(nebulaFinderService);
         		Log.i("BootUpReceiver", "NebulaFinderService başlatılıyor.");
         	}*/
    }
    
    /**
     * Bu metoda parametre olarak bir service'e ait class dosyası veriliyor. 
     * Metod geriye bu service'in çalışıp çalışmadığı bilgisini dönüyor.
     * @param serviceClass
     * @return
     */
    public boolean isMyServiceRunning(Class<?> serviceClass,Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

}
